﻿using Entity.Models.BaseEntitys;
using Entity.Models.Entities;
using IDAL;
using IDAL.IBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL.Base
{
    /// <summary>
    /// 包含删除时间的DAL接口实现类
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    public class BaseDeleteEntityDAL<T> : BaseEntityDAL<T>,IBaseDeleteEntity<T> where T : BaseDeleteEntity
    {
        RepositorySysContext _repositorySys;

        public BaseDeleteEntityDAL(RepositorySysContext repositorySys):base(repositorySys)
        {
            _repositorySys = repositorySys;
        }

        /// <summary>
        /// 根据id获取数据
        /// </summary>
        /// <returns></returns>
        public T getByEntityId(string Id)
        {
            return _repositorySys.Set<T>().FirstOrDefault(e => e.Id.EndsWith(Id) && e.IsDelete != true);
        }

        /// <summary>
        /// 软删除删除
        /// </summary>
        /// <param name="Id">id</param>
        /// <returns></returns>
        public int SoftDalateEntity(string Id)
        {
            T entity = _repositorySys.Set<T>().Find(Id);
            if (entity != null)
            {
                entity.IsDelete = true;
                entity.DeleteTime = DateTime.Now;
                _repositorySys.Set<T>().Update(entity);
            }
            return _repositorySys.SaveChanges();
        }
    }
}
